home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -in_the_mag- / emulation / macos / uae069b2.src.cpt.hqx / UAE069ß2.SRC.CPT / uae069fl2.src / keybuf.c < prev    next >
C/C++ Source or Header  |  1997-07-02  |  4KB  |  163 lines

  1.  /* 
  2.   * UAE - The Un*x Amiga Emulator
  3.   * 
  4.   * Keyboard buffer. Not really needed for X, but for SVGAlib and possibly
  5.   * Mac and DOS ports.
  6.   * 
  7.   * Note: it's possible to have two threads in UAE, one reading keystrokes
  8.   * and the other one writing them. Despite this, no synchronization effort
  9.   * is needed. This code should be perfectly thread safe. At least if you
  10.   * assume that integer store instructions are atomic.
  11.   * 
  12.   * Copyright 1995, 1997 Bernd Schmidt
  13.   */
  14.  
  15. #include "sysconfig.h"
  16. #include "sysdeps.h"
  17. #include <assert.h>
  18.  
  19. #include "config.h"
  20. #include "options.h"
  21. #include "keybuf.h"
  22. #include "keyboard.h"
  23. #include "joystick.h"
  24.  
  25. static int fakestate[3][5] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };
  26.  
  27. static int *fs_np;
  28. static int *fs_ck;
  29. static int *fs_se;
  30.  
  31. void getjoystate(int nr, unsigned int *st, int *button)
  32. {
  33.     int *fake = 0;
  34.     
  35.     if (JSEM_ISJOY0 (nr, currprefs.fake_joystick))
  36.     nr = 0;
  37.     else if (JSEM_ISJOY1 (nr, currprefs.fake_joystick))
  38.     nr = 1;
  39.     else if (JSEM_ISMOUSE (nr, currprefs.fake_joystick)) {
  40.     *st = 0;
  41.     *button = 0;
  42.     return;
  43.     } else
  44.     fake = fakestate[nr];
  45.  
  46.     if (fake) {
  47.     int top = fake[0];
  48.     int bot = fake[3];
  49.     if (fake[1]) top = !top;
  50.     if (fake[2]) bot = !bot;
  51.     *st = bot | (fake[2] << 1) | (top << 8) | (fake[1] << 9);
  52.     *button = fake[4];
  53.     } else
  54.     read_joystick (nr, st, button);
  55. }
  56.  
  57. /* Not static so the DOS code can mess with them */
  58. int kpb_first, kpb_last;
  59.  
  60. int keybuf[256];
  61.  
  62. int keys_available (void)
  63. {
  64.     int val;
  65.     val = kpb_first != kpb_last;
  66.     return val;
  67. }
  68.  
  69. int get_next_key (void)
  70. {
  71.     int key;
  72.     assert (kpb_first != kpb_last);
  73.     
  74.     key = keybuf[kpb_last];
  75.     if (++kpb_last == 256) 
  76.     kpb_last = 0;
  77.     return key;    
  78. }
  79.  
  80. void record_key (int kc)
  81. {
  82.     int kpb_next = kpb_first + 1;
  83.  
  84.     if (kpb_next == 256)
  85.     kpb_next = 0;
  86.     if (kpb_next == kpb_last) {
  87.     write_log ("Keyboard buffer overrun. Congratulations.\n");
  88.     return;
  89.     }
  90.     if (fs_np != 0) {
  91.     switch (kc >> 1) {
  92.      case AK_NP8: fs_np[0] = !(kc & 1); return;
  93.      case AK_NP4: fs_np[1] = !(kc & 1); return;
  94.      case AK_NP6: fs_np[2] = !(kc & 1); return;
  95.      case AK_NP2: fs_np[3] = !(kc & 1); return;
  96.      case AK_NP0: case AK_NP5: fs_np[4] = !(kc & 1); return;
  97.     }
  98.     }
  99.     if (fs_ck != 0) {
  100.     switch (kc >> 1) {
  101.      case AK_UP: fs_ck[0] = !(kc & 1); return;
  102.      case AK_LF: fs_ck[1] = !(kc & 1); return;
  103.      case AK_RT: fs_ck[2] = !(kc & 1); return;
  104.      case AK_DN: fs_ck[3] = !(kc & 1); return;
  105. #ifndef __mac__
  106.      case AK_RCTRL: fs_ck[4] = !(kc & 1); return;
  107. #else
  108.     case AK_CTRL: fs_ck[4] = !(kc & 1); return;
  109. #endif
  110.     }
  111.     }
  112.     if (fs_se != 0) {
  113.     switch (kc >> 1) {
  114. #ifndef __mac__
  115.      case AK_T: fs_se[0] = !(kc & 1); return;
  116.      case AK_F: fs_se[1] = !(kc & 1); return;
  117.      case AK_H: fs_se[2] = !(kc & 1); return;
  118.      case AK_B: fs_se[3] = !(kc & 1); return;
  119.      case AK_LALT: fs_se[4] = !(kc & 1); return;
  120. #else
  121.      case AK_I: fs_se[0] = !(kc & 1); return;
  122.      case AK_J: fs_se[1] = !(kc & 1); return;
  123.      case AK_L: fs_se[2] = !(kc & 1); return;
  124.      case AK_K: fs_se[3] = !(kc & 1); return;
  125.      case AK_CTRL: fs_se[4] = !(kc & 1); return;
  126. #endif
  127.     }
  128.     }
  129. #ifndef __mac__
  130.     if (kc == AK_RCTRL)
  131.     kc = AK_CTRL;
  132. #endif
  133.     keybuf[kpb_first] = kc;
  134.     kpb_first = kpb_next;
  135. }
  136.  
  137. void joystick_setting_changed (void)
  138. {
  139.     fs_np = fs_ck = fs_se = 0;
  140.  
  141.     if (JSEM_ISNUMPAD (0, currprefs.fake_joystick))
  142.     fs_np = fakestate[0];
  143.     else if (JSEM_ISNUMPAD (1, currprefs.fake_joystick))
  144.     fs_np = fakestate[1];
  145.     
  146.     if (JSEM_ISCURSOR (0, currprefs.fake_joystick))
  147.     fs_ck = fakestate[0];
  148.     else if (JSEM_ISCURSOR (1, currprefs.fake_joystick))
  149.     fs_ck = fakestate[1];
  150.  
  151.     if (JSEM_ISSOMEWHEREELSE (0, currprefs.fake_joystick))
  152.     fs_se = fakestate[0];
  153.     else if (JSEM_ISSOMEWHEREELSE (1, currprefs.fake_joystick))
  154.     fs_se = fakestate[1];
  155.     
  156. }
  157.  
  158. void keybuf_init (void)
  159. {
  160.     kpb_first = kpb_last = 0;
  161.     joystick_setting_changed ();
  162. }
  163.